/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.activities;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import gr.ebs.gssdroid.R;
import gr.ebs.gssdroid.async.GssTasks;
import gr.ebs.gssdroid.async.TaskResult;
import gr.ebs.gssdroid.resource.FolderResource;
import gr.ebs.gssdroid.resource.OtherUserResource;
import gr.ebs.gssdroid.resource.OthersResource;
import gr.ebs.gssdroid.resource.RestResource;
import gr.ebs.gssdroid.resource.SearchResource;
import gr.ebs.gssdroid.widget.CustomListView;
import gr.ebs.gssdroid.widget.MyFoldersListAdapter;

public class GssSearch extends GssMyFolders{
	SearchResource searchResource = null;
	
	protected int getContentLayoutId(){
		return R.layout.search;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ImageButton newButton = (ImageButton) findViewById(R.id.newBottomItem);
		newButton.setVisibility(View.INVISIBLE);
		ImageButton newSearchButton = (ImageButton) findViewById(R.id.newSearch);
		newSearchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final AlertDialog.Builder alert = new AlertDialog.Builder(GssSearch.this);
				alert.setTitle("Search Dialog");
				final EditText input = new EditText(GssSearch.this);
				alert.setView(input);
				alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						String value = input.getText().toString().trim();
						getTasks().getSearchTask(GssSearch.this).execute( value);
					}
				});
				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
				alert.show();
			}
		});
	}
	protected void initialize() {
		adapter = new MyFoldersListAdapter((Activity) this, R.layout.row, new ArrayList<RestResource>()) {
			public void onItemClick(final int position, final View v) {
				RestResource entry = getAdapter().getItem(position);
				RestResource toUse = entry;

				Intent intent = new Intent(getApplication(), GssProperties.class);
				intent.putExtra("resource", toUse);
				startActivityForResult(intent, 2009);

			};
		};

		((CustomListView) findViewById(R.id.fileListView)).setAdapter(adapter);
		if(getDroidApplication().getUserResource()==null||getDroidApplication().getUserResource().getFilesPath()==null){
			handleLogin();
			finish();
			return;
		}
		Log.d("INIT:", ""+getDroidApplication());
		Log.d("INIT2:", ""+getDroidApplication());
		if(searchResource==null){
			final AlertDialog.Builder alert = new AlertDialog.Builder(GssSearch.this);
			alert.setTitle("Search Dialog");
			final EditText input = new EditText(GssSearch.this);
			alert.setView(input);
			alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					String value = input.getText().toString().trim();
					getTasks().getSearchTask(GssSearch.this).execute( value);
				}
			});
			alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					dialog.cancel();
				}
			});
			alert.show();
		}
		
		//getTasks().getSearchTask(GssSearch.this).execute("mnfdroid");
	}
	
	protected void populateList(RestResource resource) {
		//currentResource=resource;
		if (resource instanceof SearchResource) {
			searchResource = (SearchResource) resource;
			((TextView) findViewById(R.id.textPath)).setText("Search Results:"+ searchResource.getQuery());
			ArrayList<RestResource> data = new ArrayList<RestResource>();
			data.addAll(searchResource.getFiles());
			adapter.setListItems(data);
			adapter.notifyDataSetChanged();
			
			ImageButton newButton = (ImageButton) findViewById(R.id.newBottomItem);
			newButton.setVisibility(View.INVISIBLE);
		} 
	}
	
	@Override
	public void handleResult(int taskId, TaskResult result) {
		if(taskId == GssTasks.TASK_SEARCH){
			populateList(result.getResource());
			return;
		}
		super.handleResult(taskId, result);
	}
	
	@Override
	protected void refresh() {
		getTasks().getSearchTask(GssSearch.this).execute(searchResource.getQuery());	
	}
}
